<template>
  <div class="app-container">
    <el-divider>待处理业务</el-divider>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="原料编号" prop="materialId" />
      <el-table-column label="材料名称" prop="materialName" />
      <el-table-column label="原料厂商" prop="farmerDept" />
      <el-table-column label="原料厂商负责人" prop="farmerNickName" />
      <el-table-column label="加工状态" prop="maching">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.machingStatus === null">未加工</el-tag>
          <el-tag
            v-show="scope.row.machingStatus === 1"
            type="danger"
          >加工中(任务已经分发)</el-tag>
          <el-tag
            v-show="scope.row.machingStatus === 2"
            type="warning"
          >加工完成</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="库状态" prop="out_factory_status">
        <template slot-scope="scope">
          <el-tag v-show="scope.row.outFactoryStatus === null">未出库</el-tag>
          <el-tag v-show="scope.row.outFactoryStatus === 1">已出库</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-show="
              scope.row.machingStatus !== 1 && scope.row.machingStatus !== 2
            "
            size="mini"
            type="text"
            @click="taskDistribution(scope.row)"
          >任务分发</el-button>
          <el-button
            v-show="scope.row.machingStatus !== null"
            size="mini"
            type="text"
            @click="queryTaskByCropsId(scope.row)"
          >查看任务分发信息</el-button>
          <el-button
            v-show="scope.row.productWriteStatus !== 1"
            size="mini"
            type="text"
            @click="addProduct(scope.row)"
          >填写产品基本信息</el-button>
          <el-button
            v-show="scope.row.productWriteStatus !== null"
            size="mini"
            type="text"
            @click="queryProductInfo(scope.row)"
          >查看产品基本信息</el-button>
          <el-button
            size="mini"
            type="text"
            @click="staffWork(scope.row)"
          >员工工作量</el-button>
          <el-button
            size="mini"
            type="text"
            @click="noticeTransport(scope.row)"
          >通知运输</el-button>
          <el-button
            v-show="scope.row.outFactoryStatus === null"
            size="mini"
            type="text"
            @click="outFactory(scope.row)"
          >产品出库</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 任务分发 -->
    <el-dialog
      center
      title="任务分发"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item
          label="任务标题"
        ><el-input
          v-model="form.taskName"
          placeholder="请输入任务标题"
        /></el-form-item>
        <el-form-item
          label="任务分工内容"
        ><el-input
          v-model="form.taskRemark"
          type="textarea"
          placeholder="请输入内容"
        /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addTask">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 生产基本信息添加 -->
    <el-dialog
      center
      title="生产基本信息添加"
      :visible.sync="addThingDialog"
      width="700px"
      append-to-body
    >
      <el-form ref="form" label-width="100px" :model="recordForm">
        <el-form-item
          label="产品名称"
          prop="nickName"
        ><el-input
          v-model="recordForm.ProductName"
          placeholder="请输入"
        /></el-form-item>
        <el-form-item
          label="型号"
          prop="nickName"
        ><el-input
          v-model="recordForm.ProductModel"
          placeholder="请输入"
        /></el-form-item>
        <el-form-item
          label="系列号"
          prop="nickName"
        ><el-input
          v-model="recordForm.ProductSerial"
          placeholder="请输入"
        /></el-form-item>
        <el-form-item
          label="尺寸"
          prop="nickName"
        ><el-input
          v-model="recordForm.ProductSize"
          placeholder="请输入"
        /></el-form-item>
        <el-form-item
          label="容量"
          prop="nickName"
        ><el-input
          v-model="recordForm.ProductCapacity"
          placeholder="请输入"
        /></el-form-item>
        <el-form-item
          label="质保期"
          prop="nickName"
        ><el-input
          v-model="recordForm.WarrantyPeriod"
          placeholder="请输入"
        /></el-form-item>
        <el-form-item label="生产批次" prop="nickName">
          <el-input v-model="recordForm.ProductBatch" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="产品图片">
          <el-upload
            class="avatar-uploader"
            :on-change="getProductImg"
            :show-file-list="false"
            :auto-upload="false"
            accept="image/*"
          >
            <img
              v-if="recordForm.imageUrl"
              :src="recordForm.imageUrl"
              style="max-width: 100%"
            >
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitProduct">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 任务下发详情 -->
    <el-dialog
      center
      title="任务下发详情"
      :visible.sync="taskInfoDialog"
      width="700px"
      append-to-body
    >
      <el-row>
        <el-col :span="12">任务ID：{{ taskInfo.taskId }}</el-col>
        <el-col :span="12">任务名称：{{ taskInfo.taskName }}</el-col>
      </el-row>
      <el-divider />
      <el-row>
        <el-col :span="12">原材料ID：{{ taskInfo.materialId }}</el-col>
        <el-col :span="12">发布时间：{{ taskInfo.time }}</el-col>
      </el-row>
      <el-divider />
      <el-row>
        <el-col :span="12">状态：{{ taskInfo.status }}</el-col>
      </el-row>
      <el-divider />
      <el-row>
        <el-col :span="24">详情内容：{{ taskInfo.taskRemark }}</el-col>
      </el-row>
    </el-dialog>
    <!-- 产品基本信息 -->
    <el-dialog
      center
      title="产品基本信息"
      :visible.sync="productInfoDialog"
      width="600px"
      append-to-body
    >
      <el-row>原料ID：{{ productInfos.material_id }}</el-row>
      <el-divider />
      <el-row>产品ID：{{ productInfos.product_id }}</el-row>
      <el-divider />
      <el-row>产品名称：{{ productInfos.product_name }}</el-row>
      <el-divider />
      <el-row>型号：{{ productInfos.product_model }}</el-row>
      <el-divider />
      <el-row>系列：{{ productInfos.product_serial }}</el-row>
      <el-divider />
      <el-row>尺寸:{{ productInfos.product_size }}</el-row>
      <el-divider />
      <el-row>容量：{{ productInfos.product_capacity }}</el-row>
      <el-divider />
      <el-row>质保期：{{ productInfos.warranty_period }}</el-row>
      <el-divider />
      <el-row>生产批次：{{ productInfos.product_batch }}</el-row>
      <el-divider />
      <el-row>数据上链时间：{{ productInfos.create_time }}</el-row>
      <el-divider />
      <el-row>
        产品图片：
        <el-image :src="productInfos.product_image" /></el-row>
    </el-dialog>
    <!-- 员工工作量证明 -->
    <el-dialog
      center
      title="产品生产信息"
      :visible.sync="staffWorkDialog"
      width="1200px"
      append-to-body
    >
      <el-row :gutter="5">
        <el-col
          v-for="(info, index) in operationList"
          :key="index"
          :span="8"
          style="padding-top: 10p;"
        >
          <el-card v-if="info.operation_type == 'Produce'" style="border-color: #42b983;min-height:200px;">
            <el-row>
              <h3>生产信息</h3>
            </el-row>
            <el-row>
              <el-col :span="24">生产ID：{{ info.operation_id }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">生产员工：{{ info.operation_name }}</el-col>
            </el-row>
            <br>
            <br />
            <el-row>
              <el-col :span="24">生产时间：{{ info.create_time }}</el-col>
            </el-row>
            <br />
            <el-row>
              <el-col :span="24">现场照片：
                <el-image :src="info.operation_image" />
              </el-col>
            </el-row>
          </el-card>

          <el-card v-if="info.operation_type == 'Quality'" style="border-color: #42b983;min-height:200px;">
            <el-row>
              <h3>质检信息</h3>
            </el-row>
            <el-row>
              <el-col :span="24">质检ID：{{ info.operation_id }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">质检结果：{{ info.quality_result }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">质检员工：{{ info.operation_name }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">质检时间：{{ info.create_time }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">现场照片：
                <el-image :src="info.operation_image" />
              </el-col>
            </el-row>
          </el-card>

          <el-card v-if="info.operation_type == 'Package'" style="border-color: #42b983;min-height:200px;">
            <el-row>
              <h3>包装信息</h3>
            </el-row>
            <el-row>
              <el-col :span="24">包装ID：{{ info.operation_id }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">包装方式：{{ info.package_method }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">包装时间：{{ info.create_time }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">包装员工：{{ info.operation_name }}</el-col>
            </el-row>
            <br>
            <el-row>
              <el-col :span="24">现场照片：
                <el-image :src="info.operation_image" />
              </el-col>
            </el-row>
          </el-card>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 联系运输 -->
    <el-dialog
      center
      title="联系运输"
      :visible.sync="noticeDetaiDialog"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="trasportForm" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="选择司机" prop="nickName">
              <el-select
                v-model="trasportForm.userName"
                placeholder="请选择"
                style="width: 370px"
              >
                <el-option
                  v-for="user in driverList"
                  :key="user.userName"
                  :label="user.nickName"
                  :value="user.userName"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="分销商" prop="nickName">
              <el-select
                v-model="trasportForm.deptId"
                placeholder="请选择"
                style="width: 370px"
              >
                <el-option
                  v-for="dept in factoryList"
                  :key="dept.deptId"
                  :label="dept.deptName"
                  :value="dept.deptId"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item
          label="备注"
        ><el-input
          v-model="trasportForm.remarks"
          type="textarea"
          placeholder="请输入内容"
        /></el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addNoticeTrasport">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRole,
  delRole,
  exportRole,
  dataScope,
} from '@/api/system/role'
import {
  treeselect as menuTreeselect,
  roleMenuTreeselect
} from '@/api/system/menu'
import {
  treeselect as deptTreeselect,
  roleDeptTreeselect
} from '@/api/system/dept'
import {
  productOutFactory,
  updateProductWriteStatus,
  queryTaskByCropsId,
  queryProductList,
  addTask,
  updateMachingStatus
} from '../../../api/trace/product'
import { addProduct } from '../../../api/trace/user-product'
import { formatDate } from '../../../utils/index.js'
import {
  getFactoryByDeptId,
  getAllDriverByDeptId,
  addTransport
} from '../../../api/trace/farmer'

import { uploadImg } from '@/api/common.js'

export default {
  name: 'Role',

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      productList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 状态数据字典
      statusOptions: ['在种', '停种'],

      beggingOptions: [],
      plantModeOptions: [], // 种植方式
      cropsTypeOptions: [], // 作物类型

      growDialog: false, // z作物生长记录弹出框
      cropsName: '',
      cropsDetaiDialog: false,

      cropsDetails: '',
      // 菜单列表
      menuOptions: [],
      // 部门列表
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleName: undefined,
        roleKey: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      recordForm: {},
      imageUrl: '',
      imgBase64: [],
      cropsPhotoUrl: '',
      cropsInfo: '',
      growDialog: false,
      cropsProcessDetailsArray: [],
      cropsProcessDetaiDialog: false,
      noticeDetaiDialog: false,
      driverList: [],
      trasportForm: {
        materialId: '',
        farmerUserName: '',
        farmerNickName: '',
        time: ''
      },
      trasportInfo: '',
      playTrackView: false,
      addThingDialog: false,
      taskInfo: '',
      taskInfoDialog: false,
      materialInfos: '',
      productInfoDialog: false,
      productInfos: '',
      staffWorkDialog: false,
      operationList: [],
      driverList: [],
      factoryList: [],
      produceInfo: '',
      packageInfo: '',
      qualityInfo: ''
    }
  },
  created() {
    this.getProductList()
    this.getDicts('crops_bagging').then((response) => {
      this.beggingOptions = response.data
    })
    this.getDicts('crops_plant_type').then((res) => {
      this.plantModeOptions = res.data
    })
    this.getDicts('crops_type').then((res) => {
      this.cropsTypeOptions = res.data
    })
  },
  methods: {
    outFactory(row) {
      this.$confirm(
        '确定将ID为【' + row.materialId + '】的产品出库吗',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          productOutFactory(row.materialId).then(() => {
            this.getProductList()
            this.$message({
              type: 'success',
              message: '已出库!'
            })
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消出库'
          })
        })
    },

    addNoticeTrasport() {
      this.trasportForm.materialId = this.checkInfo.materialId
      this.trasportForm.farmerUserName = this.$store.getters.name
      this.trasportForm.farmerNickName = this.$store.getters.nickName
      this.trasportForm.time = new Date()
      this.trasportForm.driverId = this.trasportForm.userName
      this.trasportForm.status = 0
      this.trasportForm.farmerTel = this.$store.getters.phonenumber
      this.trasportForm.farmerDept = this.$store.getters.deptName
      this.trasportForm.factoryId = this.trasportForm.deptId
      addTransport(this.trasportForm)
        .then((res) => {
          this.msgSuccess('联系运输成功')
          this.noticeDetaiDialog = false
        })
        .catch((err) => {
          this.msgError('联系运输失败，发生异常')
        })
    },

    noticeTransport(row) {
      this.checkInfo = row
      this.noticeDetaiDialog = true
      getAllDriverByDeptId(this.$store.getters.deptId)
        .then((res) => {
          this.driverList = res.data
        })
        .catch((err) => {})
      getFactoryByDeptId(125)
        .then((res) => {
          this.factoryList = res.data
        })
        .catch((err) => {})
    },

    staffWork(row) {
      this.staffWorkDialog = true
      this.$httpBlock
        .get(
          this.$httpUrl +
            '/productapi/queryOperationByCropsId?id=' +
            row.materialId
        )
        .then((res) => {
          const array = []
          for (let i = 0; i < res.data.length; i++) {
            array.push(res.data[i].Record)
          }
          this.operationList = array
        })
        .catch((err) => {
          this.msgError('查询异常 ' + err)
        })
    },

    queryProductInfo(row) {
      this.productInfoDialog = true
      this.$httpBlock
        .get(
          this.$httpUrl +
            '/productapi/queryProductInfoByCropsId?id=' +
            row.materialId
        )
        .then((res) => {
          this.productInfos = res.data[0].Record
        })
        .catch((err) => {
          this.msgError('查询异常 ' + err)
        })
    },

    submitProduct() {
      const productInfoArray = []
      const id = new this.$snowFlakeId().generate()
      productInfoArray.push(id)
      productInfoArray.push(id)
      productInfoArray.push(this.materialInfos.materialId)
      productInfoArray.push(this.recordForm.ProductName)
      productInfoArray.push(this.recordForm.ProductModel)
      productInfoArray.push(this.recordForm.ProductSerial)
      productInfoArray.push(this.recordForm.ProductSize)
      productInfoArray.push(this.recordForm.ProductCapacity)
      productInfoArray.push(this.recordForm.WarrantyPeriod)
      productInfoArray.push(this.recordForm.ProductBatch)
      productInfoArray.push(this.recordForm.imageUrl)
      productInfoArray.push(formatDate(new Date()))
      productInfoArray.push(this.$store.getters.deptName)
      this.$httpBlock
        .post(this.$httpUrl + '/productapi/createProductInfo', {
          productInfoArray: productInfoArray
        })
        .then((res) => {
          if (res.status === 200) {
            this.msgSuccess('数据上链成功')
            const product = {
              productId: id,
              productName: this.recordForm.ProductName,
              materialId: this.materialInfos.materialId,
              materialName: this.materialInfos.materialName,
              userName: this.$store.getters.name,
              sourceId: id
            }
            addProduct(product)
            updateProductWriteStatus(this.materialInfos.materialId).then(
              (res) => {
                this.addThingDialog = false
                this.msgSuccess('数据更新成功')
                this.getProductList()
              }
            )
          }
        })
        .catch((err) => {
          this.msgError('存储异常 ' + err)
        })
    },

    /**
     * 根据cropsId查询任务详情
     */
    queryTaskByCropsId(row) {
      queryTaskByCropsId(row.materialId)
        .then((res) => {
          this.taskInfo = res.data
          this.taskInfoDialog = true
        })
        .catch((err) => {
          this.msgError('查询异常')
        })
    },

    addProduct(row) {
      this.materialInfos = row
      this.addThingDialog = true
    },

    /**
     * 确定发布
     */
    addTask() {
      debugger
      this.form.taskId = new this.$snowFlakeId().generate()
      this.form.materialId = this.productInfo.materialId
      this.form.leaderId = this.$store.getters.name
      this.form.time = formatDate(new Date())
      this.form.leaderName = this.$store.getters.nickName
      this.form.status = 0
      addTask(this.form)
        .then((res) => {
          const info = {
            materialId: this.productInfo.materialId,
            machingStatus: 1
          }
          updateMachingStatus(info)
            .then((res) => {
              this.msgSuccess('产品进入加工状态')
              this.getProductList()
            })
            .catch((err) => {})
          this.msgSuccess('发布任务成功')
          this.open = false
        })
        .catch((err) => {
          this.msgError('添加任务出错')
        })
    },
    /**
     * 任务分发
     */
    taskDistribution(row) {
      this.productInfo = row
      this.open = true
    },

    getFile(file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.getBase64(file.raw).then((res) => {
        this.imgBase64 = res
        const datas = {
          imageBase64: this.imgBase64
        }
        uplodImagesBase64(datas)
          .then((res) => {
            this.cropsPhotoUrl = res.msg
            this.msgSuccess('图片上传成功')
          })
          .catch((err) => {
            this.msgError('图片上传失败')
          })
      })
    },

    getBase64(file) {
      return new Promise(function(resolve, reject) {
        const reader = new FileReader()
        let imgResult = ''
        reader.readAsDataURL(file)
        reader.onload = function() {
          imgResult = reader.result
        }
        reader.onerror = function(error) {
          reject(error)
        }
        reader.onloadend = function() {
          resolve(imgResult)
        }
      })
    },

    handleRecord(row) {
      this.cropsName = row.cropsName
      this.cropsInfo = row
      this.growDialog = true
    },

    /** 查询作物列表 */
    getProductList() {
      this.loading = true
      queryProductList(this.$store.getters.deptId).then((res) => {
        this.productList = res.rows
        this.total = res.total
        this.loading = false
      })
    },

    /**
     * 作物详情
     */
    cropsDetail(row) {
      this.$httpBlock
        .get(this.$httpUrl + '/farmerapi/queryCropsById?id=' + row.materialId)
        .then((res) => {
          this.cropsDetails = res.data
          this.cropsDetaiDialog = true
        })
        .catch((err) => {})
    },

    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then((response) => {
        this.menuOptions = response.data
      })
    },
    /** 查询部门树结构 */
    getDeptTreeselect() {
      deptTreeselect().then((response) => {
        this.deptOptions = response.data
      })
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      const checkedKeys = this.$refs.menu.getHalfCheckedKeys()
      // 半选中的菜单节点
      const halfCheckedKeys = this.$refs.menu.getCheckedKeys()
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys)
      return checkedKeys
    },
    // 所有部门节点数据
    getDeptAllCheckedKeys() {
      // 目前被选中的部门节点
      const checkedKeys = this.$refs.dept.getHalfCheckedKeys()
      // 半选中的部门节点
      const halfCheckedKeys = this.$refs.dept.getCheckedKeys()
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys)
      return checkedKeys
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeselect(roleId) {
      return roleMenuTreeselect(roleId).then((response) => {
        this.menuOptions = response.menus
        return response
      })
    },
    /** 根据角色ID查询部门树结构 */
    getRoleDeptTreeselect(roleId) {
      return roleDeptTreeselect(roleId).then((response) => {
        this.deptOptions = response.depts
        return response
      })
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false
      this.reset()
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([])
      }
      (this.menuExpand = false),
      (this.menuNodeAll = false),
      (this.deptExpand = true),
      (this.deptNodeAll = false),
      (this.form = {
        roleId: undefined,
        roleName: undefined,
        roleKey: undefined,
        roleSort: 0,
        status: '0',
        menuIds: [],
        deptIds: [],
        menuCheckStrictly: true,
        deptCheckStrictly: true,
        remark: undefined
      })
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    getList() {},
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.roleId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    // 树权限（展开/折叠）
    handleCheckedTreeExpand(value, type) {
      if (type == 'menu') {
        const treeList = this.menuOptions
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.menu.store.nodesMap[treeList[i].id].expanded = value
        }
      } else if (type == 'dept') {
        const treeList = this.deptOptions
        for (let i = 0; i < treeList.length; i++) {
          this.$refs.dept.store.nodesMap[treeList[i].id].expanded = value
        }
      }
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value, type) {
      if (type == 'menu') {
        this.$refs.menu.setCheckedNodes(value ? this.menuOptions : [])
      } else if (type == 'dept') {
        this.$refs.dept.setCheckedNodes(value ? this.deptOptions : [])
      }
    },
    // 树权限（父子联动）
    handleCheckedTreeConnect(value, type) {
      if (type == 'menu') {
        this.form.menuCheckStrictly = !!value
      } else if (type == 'dept') {
        this.form.deptCheckStrictly = !!value
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getMenuTreeselect()
      this.open = true
      this.title = '记录作物'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const roleId = row.roleId || this.ids
      const roleMenu = this.getRoleMenuTreeselect(roleId)
      getRole(roleId).then((response) => {
        this.form = response.data
        this.open = true
        this.$nextTick(() => {
          roleMenu.then((res) => {
            this.$refs.menu.setCheckedKeys(res.checkedKeys)
          })
        })
        this.title = '修改作物'
      })
    },
    /** 分配数据权限操作 */
    handleDataScope(row) {
      this.reset()
      const roleDeptTreeselect = this.getRoleDeptTreeselect(row.roleId)
      getRole(row.roleId).then((response) => {
        this.form = response.data
        this.openDataScope = true
        this.$nextTick(() => {
          roleDeptTreeselect.then((res) => {
            this.$refs.dept.setCheckedKeys(res.checkedKeys)
          })
        })
        this.title = '分配数据权限'
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      // this.$refs["form"].validate(valid => {
      //     if (valid) {
      addCrops(this.form).then((res) => {
        if (res.code === 200) {
          this.msgSuccess('修改成功')
          this.open = false
        }
      })
      // if (this.form.roleId != undefined) {
      //     this.form.menuIds = this.getMenuAllCheckedKeys();
      //     updateRole(this.form).then(response => {
      //         if (response.code === 200) {
      //             this.msgSuccess("修改成功");
      //             this.open = false;
      //             this.getList();
      //         }
      //     });
      // } else {
      //     this.form.menuIds = this.getMenuAllCheckedKeys();
      //     addRole(this.form).then(response => {
      //         if (response.code === 200) {
      //             this.msgSuccess("新增成功");
      //             this.open = false;
      //             this.getList();
      //         }
      //     });
      // }
      //     }
      // });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function() {
      if (this.form.roleId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys()
        dataScope(this.form).then((response) => {
          if (response.code === 200) {
            this.msgSuccess('修改成功')
            this.openDataScope = false
            this.getList()
          }
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.roleId || this.ids
      this.$confirm(
        '是否确认删除角色编号为"' + roleIds + '"的数据项?',
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delRole(roleIds)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function() {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有角色数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportRole(queryParams)
        })
        .then((response) => {
          this.download(response.msg)
        })
        .catch(function() {})
    },

    getProductImg(file) {
      const data = new FormData()
      data.append('file', file.raw)
      uploadImg(data).then((res) => {
        this.$set(
          this.recordForm,
          'imageUrl',
          process.env.VUE_APP_BASE_API + res.imgurl
        )
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.image {
  width: 80%;
  display: block;
}
/**一定要给宽高，否则不显示*/
#position {
  height: 100%;
}
/**修改点标记图片的css, 图片给宽高，否则不显示 */
.map-page /deep/ {
  .amap-icon {
    width: 20px;
  }
  .amap-marker-label {
    border: 1px solid #ccc;
    font-size: 16px;
    display: inline-block;
    padding: 5px;
  }
}

#panel {
  position: fixed;
  background-color: white;
  max-height: 90%;
  overflow-y: auto;
  top: 10px;
  right: 10px;
  width: 280px;
}
#panel .amap-call {
  background-color: #009cf9;
  border-top-left-radius: 4px;
  border-top-right-radius: 4px;
}
#panel .amap-lib-driving {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  overflow: hidden;
}
html,
body,
#container {
  width: 100%;
  height: 100%;
}

.map {
  width: 100%;
  height: 500px;
}
</style>
